//
//  HCRemarkListModel.m
//  HelloCall
//
//  Created by WangYongpeng on 13-11-15.
//  Copyright (c) 2013年 HC. All rights reserved.
//

#import "HCRemarkListModel.h"
#import "HCNetworkManger.h"

@implementation HCCallRemarkInfo

//@synthesize mobliePhoneNumber = m_mobliePhoneNumber;
//@synthesize phoneNumber = m_phoneNumber;
//@synthesize contact = m_contact;
//@synthesize company = m_company;
//@synthesize qqNumber = m_qqNumber;
//@synthesize web = m_web;
//@synthesize nextVisitTime = m_nextVisitTime;
//@synthesize memo = m_memo;
@synthesize custID = m_custID;                      //客户ID
@synthesize content = m_content;                    //备注信息
@synthesize memoId = m_memoId;                      //备注ID
@synthesize ticketId = m_ticketId;                  //话单ID
@synthesize addMemoUserID = m_addMemoUserID;        //添加者ID
@synthesize addMemoDate = m_addMemoDate;            //添加备忘时间
@synthesize lastModifyDate = m_lastModifyDate;      //上次修改时间
@synthesize contactTime = m_contactTime;            //沟通时间


- (void)deallo
{
    self.custID = nil;
    self.content = nil;
    self.addMemoUserID = nil;
    self.addMemoDate = nil;
    self.lastModifyDate = nil;
    self.contactTime = nil;
    self.ticketId = nil;
    
    [super dealloc];
}

@end


@interface HCRemarkListModel()<HCNetworkObserver>

@property (nonatomic, retain) NSMutableArray* remarkList;

@end

@implementation HCRemarkListModel

@synthesize remarkList = m_remarkList;
static HCRemarkListModel* g_defaultModel = nil;

+ (HCRemarkListModel*)defaultModel
{
    if (nil == g_defaultModel)
    {
        g_defaultModel = [[HCRemarkListModel alloc] init];
    }
    return g_defaultModel;
}

- (id)init
{
    self = [super init];
    if (self != nil)
    {
        [[HCNetworkManger shareManger] addObserver:self];
    }
    return self;
}

- (void)dealloc
{
    [[HCNetworkManger shareManger] removeObserver:self];
    g_defaultModel = nil;
    
    self.remarkList = nil;
    
    [super dealloc];
}

- (NSArray*)getRemarkListByCustId:(NSString*)custID
{
    NSMutableArray* result = [NSMutableArray arrayWithCapacity:0];
    
    for (HCCallRemarkInfo* info in self.remarkList)
    {
        if ([info.custID isEqualToString:custID])
        {
            [result addObject:info];
        }
    }
    
    return result;
}

- (NSArray*)getRemarkListByTicketId:(NSString*)ticketId
{
    NSMutableArray* result = [NSMutableArray arrayWithCapacity:0];
    
    for (HCCallRemarkInfo* info in self.remarkList)
    {
        if ([info.ticketId isEqualToString:ticketId])
        {
            [result addObject:info];
        }
    }
    
    return result;
}

- (HCCallRemarkInfo*)getRemarkInfoByMemoId:(int)memoID
{
    for (HCCallRemarkInfo* info in self.remarkList)
    {
        if (info.memoId == memoID)
        {
            return info;
        }
    }
    
    return nil;
}

- (BOOL)addRemarkInfo:(HCCallRemarkInfo*)info
{
    if ([self getRemarkInfoByMemoId:info.memoId] != nil)
    {
        return NO;
    }
    
    [self.remarkList addObject:info];
    return YES;
}

#pragma mark -- HCNetworkObserver
- (void)notifyNetworkMsg:(int)msgID data:(void*) data
{
    /*pause modify———————————————------
     
    switch (msgID)
    {
        //获取备注
        case EQueryCallMemo:
        {
            queryCallMemoResp* resp = (queryCallMemoResp*)data;
            
            if (!resp->headResp.success)
            {
                //显示错误提示
                break;
            }
            
            for (int i = 0; i < resp->vecBean.size(); i++)
            {
                NSString* custID = [NSString stringWithFormat:@"%d", resp->vecBean.at(i).custId];

                HCCallRemarkInfo* info = [[HCCallRemarkInfo alloc] init];
                info.custID = custID;
                info.content = [NSString stringWithUTF8String:resp->vecBean.at(i).content.data()];
                info.memoId = resp->vecBean.at(i).memoId;
                info.ticketId = [NSString stringWithFormat:@"%d", resp->vecBean.at(i).ticketId];
                info.addMemoUserID = [NSString stringWithFormat:@"%d", resp->vecBean.at(i).user_id];
                NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                info.addMemoDate = [dateFormatter dateFromString:[NSString stringWithUTF8String:resp->vecBean.at(i).add_time.data()]];
                info.lastModifyDate = [dateFormatter dateFromString:[NSString stringWithUTF8String:resp->vecBean.at(i).last_modify.data()]];
                info.contactTime = [dateFormatter dateFromString:[NSString stringWithUTF8String:resp->vecBean.at(i).contact_time.data()]];
                [dateFormatter release];
                
                [self addRemarkInfo:info];
            }
            
            [self sendDataUpdateEvent:msgID];
            break;
        }
        //添加备注
        case ERecordCallMemo:
        {
            break;
        }
        //修改备注
        case EUpdateCallMemo:
        {
            break;
        }
        default:
            break;
    }
     pause modify———————————————————*/
}

@end
